package cn.edu.zafu.bigdata.psy;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

// 学生类
class Student {
    private String id;
    private String name;
    private String phone;

    public Student(String id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }

    // 重写toString方法展示学生信息
    @Override
    public String toString() {
        return "学号: " + id + ", 姓名: " + name + ", 电话: " + phone;
    }

    // Getter和Setter方法
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }
}

// 学生信息管理系统类
public class StudentManagementSystem {
    private static final String FILE_NAME = "students.dat";
    private List<Student> students;

    public StudentManagementSystem() {
        students = loadStudentsFromFile();
    }

    // 从文件中加载学生信息
    private List<Student> loadStudentsFromFile() {
        List<Student> studentList = new ArrayList<>();
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME))) {
            studentList = (List<Student>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            // 文件不存在或读取错误时，返回空列表
            System.out.println("加载学生信息失败，将创建新的学生列表。");
        }
        return studentList;
    }

    // 将学生信息保存到文件
    private void saveStudentsToFile() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
            oos.writeObject(students);
        } catch (IOException e) {
            System.out.println("保存学生信息失败: " + e.getMessage());
        }
    }

    // 添加学生信息
    private void addStudent() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();

        // 检查学号是否重复
        if (isIdExists(id)) {
            System.out.println("学号已存在，请重新输入。");
            return;
        }

        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();

        Student student = new Student(id, name, phone);
        students.add(student);
        saveStudentsToFile();
        System.out.println("学生信息添加成功。");
    }

    // 检查学号是否存在
    private boolean isIdExists(String id) {
        for (Student student : students) {
            if (student.getId().equals(id)) {
                return true;
            }
        }
        return false;
    }

    // 检索学生信息
    private void searchStudent() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要检索的学号（输入QUIT退出）: ");
        String id = scanner.nextLine();

        while (!id.equals("QUIT")) {
            Student student = findStudentById(id);
            if (student!= null) {
                System.out.println(student);
            } else {
                System.out.println("未找到该学生");
            }

            System.out.print("请输入要检索的学号（输入QUIT退出）: ");
            id = scanner.nextLine();
        }
    }

    // 根据学号查找学生
    private Student findStudentById(String id) {
        for (Student student : students) {
            if (student.getId().equals(id)) {
                return student;
            }
        }
        return null;
    }

    // 更新学生信息
    private void updateStudent() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要更新信息的学号（输入QUIT退出）: ");
        String id = scanner.nextLine();

        while (!id.equals("QUIT")) {
            Student student = findStudentById(id);
            if (student!= null) {
                System.out.print("请输入新的姓名: ");
                String newName = scanner.nextLine();
                System.out.print("请输入新的电话: ");
                String newPhone = scanner.nextLine();

                student.setName(newName);
                student.setPhone(newPhone);
                saveStudentsToFile();
                System.out.println("学生信息更新成功。");
            } else {
                System.out.println("未找到该学生");
            }

            System.out.print("请输入要更新信息的学号（输入QUIT退出）: ");
            id = scanner.nextLine();
        }
    }

    // 删除学生信息
    private void deleteStudent() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要删除的学号（输入QUIT退出）: ");
        String id = scanner.nextLine();

        while (!id.equals("QUIT")) {
            Student student = findStudentById(id);
            if (student!= null) {
                students.remove(student);
                saveStudentsToFile();
                System.out.println("学生信息删除成功。");
            } else {
                System.out.println("未找到该学生");
            }

            System.out.print("请输入要删除的学号（输入QUIT退出）: ");
            id = scanner.nextLine();
        }
    }

    public static void main(String[] args) {
        StudentManagementSystem system = new StudentManagementSystem();
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    system.addStudent();
                    break;
                case 2:
                    system.searchStudent();
                    break;
                case 3:
                    system.updateStudent();
                    break;
                case 4:
                    system.deleteStudent();
                    break;
                case 5:
                    system.saveStudentsToFile();
                    System.out.println("程序已退出。");
                    return;
                default:
                    System.out.println("无效的操作编号，请重新输入。");
            }
        }
    }
}